<template>
  <div
    v-show="showMask"
    class="dialog"
  >
    <div class="dialog-container">
      <div class="dialog-title">
        {{ title }}
      </div>
      <div
        class="content"
      >
        <el-form ref="form" :model="formData" label-width="80px">
          <el-form-item label="工作地点">
            <el-input v-model="formData.RB_GZJD_NAME"></el-input>
          </el-form-item>
          <el-form-item label="归属日期">
              <el-date-picker
              v-model="formData.RB_GSRQ"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="formData.RB_BZ" :rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="btns">
        <div
          @click="closeBtn"
          class="default-btn"
        >
          {{ cancelText }}
        </div>
        <div
          @click="confirmBtn"
          class="confirm-btn"
        >
          {{ confirmText }}
        </div>
      </div>
      <div
        @click="closeMask"
        class="close-btn"
      >
        <i class="iconfont icon-close" />
      </div>
    </div>
  </div>
</template>


<script>
// import ChartBar from './components/chart-bar';
import dayjs from 'dayjs';
export default {
  name: 'LogForm',
  components: {
  },
  props: {
    params: {
      type: Object,
    },
    // 类型包括 defalut 默认， danger 危险， confirm 确认，
    callback: {
      type: Function,
    },
  },
  data() {
    return {
      showMask: true,
      title: '添加数据',
      cancelText: '关闭',
      confirmText:'保存',
       formData: {
          RB_GZJD_NAME:'',
          RB_GSRQ:'',
          RB_BZ: '',
        }
    };
  },
  watch: {
    value(newVal, oldVal) {
      this.showMask = newVal;
    },
    showMask(val) {
      this.$emit('input', val);
    },
  },
  mounted() {
    this.showMask = true;
    if(this.params.type == 'edit'){
      this.title = '编辑数据';
      this.formData.RB_GZJD_NAME=this.params.record.RB_GZJD_NAME || '';
      this.formData.RB_GSRQ=this.params.record.RB_GSRQ || '';
      this.formData.RB_BZ=this.params.record.RB_BZ || '';
    }
  },
  methods: {
    closeMask() {
      this.showMask = false;
    },
    closeBtn() {
      this.$emit('cancel');
      this.closeMask();
    },
    dangerBtn() {
      this.$emit('danger');
      this.closeMask();
    },
    /**
     * 保存数据方法
     */
    confirmBtn() {
      if(this.params && this.params.type == 'edit'){
        JE.ajax({
        url:'/je/doUpdate',
        params:{
          funcCode: 'A_RB',
          tableCode: 'A_RB',
          A_RB_ID:this.params.record.A_RB_ID,
          RB_GSRQ:dayjs(this.formData.RB_GSRQ).format('YYYY-MM-DD'),
          RB_GZJD_NAME:this.formData.RB_GZJD_NAME,
          RB_BZ:this.formData.RB_BZ
        },
        success:(obj)=>{
          const responseText = JE.decode(obj.responseText);
          if(responseText.success){
            this.closeMask();
            this.callback();
            JE.msg('保存成功！');
          }else{
            JE.alert('保存失败！');
          }
          
        }
      });
      }
      else{
        JE.ajax({
        url:'/je/doSave',
        params:{
          funcCode: 'A_RB',
          tableCode: 'A_RB',
          RB_GSRQ:dayjs(this.formData.RB_GSRQ).format('YYYY-MM-DD'),
          RB_GZJD_NAME:this.formData.RB_GZJD_NAME,
          RB_BZ:this.formData.RB_BZ
        },
        success:(obj)=>{
          const responseText = JE.decode(obj.responseText);
          if(responseText.success){
            this.closeMask();
            this.callback();
            JE.msg('保存成功！');
          }else{
            JE.alert('保存失败！');
          }
          
        }
      });
      }
      
    },
  },

};
</script>

<style lang="less" scoped>
    .dialog{
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, 0.6);
        z-index: 2;
        .dialog-container{
            width: 500px;
            height: 380px;
            background: #ffffff;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            border-radius: 8px;
            position: relative;
            .dialog-title{
                width: 100%;
                height: 60px;
                font-size: 18px;
                color: #696969;
                font-weight: 600;
                padding: 16px 50px 0 20px;
                box-sizing: border-box;
            }
            .content{
                color: #797979;
                line-height: 26px;
                padding: 0 20px;
                box-sizing: border-box;
            }
            .inp{
                margin: 10px 0 0 20px;
                width: 200px;
                height: 40px;
                padding-left: 4px;
                border-radius: 4px;
                border: none;
                background: #efefef;
                outline: none;
                &:focus{
                    border: 1px solid #509EE3;
                }
            }
            .btns{
                width: 100%;
                height: 60px;
                // line-height: 60px;
                position: absolute;
                bottom: 0;
                left: 0;
                text-align: right;
                padding: 0 16px;
                box-sizing: border-box;
                & > div{
                    display: inline-block;
                    height: 40px;
                    line-height: 40px;
                    padding: 0 14px;
                    color: #ffffff;
                    background: #f1f1f1;
                    border-radius: 8px;
                    margin-right: 12px;
                    cursor: pointer;
                }
                .default-btn{
                    color: #787878;
                    &:hover{
                        color: #509EE3;
                    }
                }
                .danger-btn{
                    background: #EF8C8C;
                    &:hover{
                        background: rgb(224, 135, 135);
                    }
                    &:active{
                        background: #EF8C8C;
                    }
                }
                .confirm-btn{
                    color: #ffffff;
                    background: #509EE3;
                    &:hover{
                        background: #6FB0EB;
                    }
                }
            }
            .close-btn{
                position: absolute;
                top: 16px;
                right: 16px;
                width: 30px;
                height: 30px;
                line-height: 30px;
                text-align: center;
                font-size: 18px;
                cursor: pointer;
                &:hover{
                    font-weight: 600;
                }
            }
        }
    }
</style>